# -*- coding:utf-8 -*-
import MySQLdb
import multiprocessing
from pyspider.libs.utils import md5string
# from selectors import DefaultSelector, EVENT_WRITE
# import socket


def sortIntegers(A):
    # write your code here
    for i in range(1, len(A)):
        temp = A[i]
        index = i - 1
        while index >= 0:
            if A[index] > temp:
                A[index+1] = A[index]
                A[index] = temp
            index -= 1
        # A[i-1] = temp
    return A

print(sortIntegers([3,2,4,1,5]))

# def fibonacci(n):
#     # write your code here
#     i, a, b = 1, 0, 1
#     while i < n:
#         a, b = b, a + b
#         i += 1
#     return a

# fibonacci(15)

# selector = DefaultSelector()
# sock = socket.socket()
# sock.setblocking(False)
# # 设置socket为阻塞或非阻塞
# # sock.setblocking(True) is equivalent to sock.settimeout(None)
# # sock.setblocking(False) is equivalent to sock.settimeout(0.0)
# try:
#   sock.connect(('xkcd.com', 80))  # 仅仅发送连接请求
# except BlockingIOError:
#   pass
# # 设置socket为非阻塞必然抛出异常
# def connected():
#   selector.unregister(sock.fileno())
#   print('connected!')
# # 注册回调（文件描述符， 事件， 回调函数）
# selector.register(sock.fileno(), EVENT_WRITE, connected)
# #
# def loop():
#   while True:
#       events = selector.select()
#       for event_key, event_mask in events:
#           callback = event_key.data
#           callback()




# multiprocessing.cpu_count()

# cnx = MySQLdb.connect(host='192.168.1.5',user='root',passwd='w111111',db='rinwoo_homepage', port=3306, charset='utf8')
# cursor = cnx.cursor()
#
# a = '晶门科技推出突破性 TDDI IC 迎合最新18:9无边框智能电话潮流'
# v = md5string(a)
# query_sql = ("select count(*) from information_md5 where md5='%s'")%(v)
# cursor.execute(query_sql)
# if (int(cursor.fetchone()[0]) > 0):
#     print 1
# else:
#     print 0



# a = ([1, ], [75, ])
# a[0].append(2)
# a[1].append(55)
# print a

# la = [1,3,5,7,9]
# lb = [2,3,5,8,15,16,19]
# lc = []
# i, j, k = 0, 0, 0
# while i < len(la) and j < len(lb):
#     ai = la[i]
#     bj = lb[j]
#     if ai <= bj:
#         lc.append(ai)
#         i += 1
#         k += 1
#     else:
#         lc.append(bj)
#         j += 1
#         k += 1
#
# while i < len(la):
#     lc.append(la[i])
#     i += 1
#     k += 1
# while j < len(lb):
#     lc.append(lb[j])
#     j += 1
#     k += 1
#
# print lc

###########################原分包策略
# run_data_hex = hex_str[4:pack_len*4]  # 获取一条
# new_data_hex = hex_str[4:]  # 去除包头的hex数据
# data = binascii.a2b_hex(new_data_hex)  # 将hex数据转回字符串

# run_data = data[:pack_len]  # 一条数据
# data = data[pack_len:]

# format_len = ((len(data) - 1 * 4) - pack_len)
# pack_format = 'I%ds%ds' % (pack_len, format_len)
#
# pack_len, runData, data = struct.unpack(pack_format, data)
# realy_pack_len = len(data)
# # self.factory.filter.requestCheck(data, self)



# strbuffer = 'BS114514,lron,12345678,2e0160e078aa4b925e62b20610378253'
# newarr = []
# i = 0
# r = 0
# for s in strbuffer:
#     if s == ',':
#         i = 0
#         r += 1
#     try:
#         newarr[i] = s
#     except:
#         newarr.append(s)
#
#     i += 1
#
# print newarr

# import hmac
#
# hmacKey = hmac.new("2e0160e078aa4b925e62b20610378253")
# hmacKey.update("2e0160e078aa4b925e62b20610378253")
# sh = hmacKey.hexdigest()
#
# print sh

"""
原任务进程，任务分配模块，对应mysql
"""
# def taskHandler(self, tasks):  # 分配任务
#     for task in tasks:
#         targetId, taskNum, need, state = task["targetId"], task["taskNum"], task["targetId"], task["state"]
#         if need == state or targetId[0:2] == "SE":  # 任务需求/当前路灯状态, 群发任务
#             self.finishTask.append(task)
#         elif taskNum >= 3:  # 执行次数>3 故障
#             self.faultTask.append(task)
#         else:  # 继续执行的
#             self.executeTask.append(task)


"""
原besd函数，灯端任务插入判断部分
"""
# if lc_tuple and all_task:  # 有需要开启的路灯，同时存在已执行的任务
#     up_lc_task = []  # 需要更新的单发任务id列表
#     up_se_task = []  # 需要更新的群发任务id列表
#     A = set()  # 拥有待执行任务的灯端集合
#     B = set(stringUtil.tuple_to_sqlArr(lc_tuple))  # 需要更改状态的灯端集合
#     for each in rl_task:  # 提取任务id和目标id
#         each = each.split(",")
#         if each[3][0:2] == "SE":  # each[3] 目标id，街道
#             up_se_task.append(each[0])  # each[0] 任务id
#         else:  # 灯端
#             up_lc_task.append(each[0])
#         A.add(each[3])
#
#     if len(up_lc_task) != 0:
#         self.inside_update(up_lc_task, 1)
#     if len(up_se_task) != 0:
#         self.inside_update(up_se_task, 2)
#
#     # 提取所有状态不合的灯端，存入单发任务
#     inList = list(B.difference(A))
#     if len(inList) != 0:
#         self.insert_task(inList, need)
# elif lc_tuple and len(rl_task) == 0:  # 有需要开启的路灯，没有存在任何形式的群发任务
#     for each in ss_task:  # 如果存在开关灯的群发任务，则不存入
#         cd = each.split(",")[1]  # 待执行的命令
#         if cd[0:3] == "seo":
#             return
#     self.insert_task(seId, need)  # 插入新的群发任务
# elif not lc_tuple and rl_task:  # 没有路灯需要变动，但存在已执行任务
#     self.inside_update(rl_task, 2)  # 完成

# def inside_update(self, seon_task, state):  # 更新seon任务为已完成
#     taskIdStr = stringUtil.arr_to_sqlStr(seon_task)
#     sql = sqlBuild.sqlDict["update_task_bellMode"] % (state, taskIdStr)
#     self.db.update(sql)

# def insert_task(self, data, need):  # 插入任务
#     inHead = sqlBuild.sqlDict["insert_task"]
#     inGroove = "('" + str(need) + "', '%s', 0, 0, CURRENT_TIMESTAMP())"
#
#     finalSql = sqlBuild.buildInsertSql(inHead=inHead, inGroove=inGroove, data=data)
#
#     self.db.insert(finalSql)


#
# # 原任务sqlbuild函数
# def buildUpdateSql(self, data, taskState=None, taskNum=None, insertSql=sqlBuild.sqlDict["update_task"]):
#     taskValueGroove = "('%s', '%s', '%s', '%s', '%s', '%s', CURRENT_TIMESTAMP())"
#     valuesSql = ""
#     length = len(data)
#     i = 0
#     for each in data:
#         #  # 0:任务id 1:任务需求 2:目标id 3:任务状态 4:任务执行次数(除完成外，任务执行次数需要+1) 5:创建日期
#         valueNail = (each[0],
#                      each[1],
#                      each[2],
#                      2 if taskState is None else taskState,
#                      each[4] if taskNum is None else each[4] + 1,
#                      each[5])
#         if i == length - 1:
#             valuesSql = valuesSql + (taskValueGroove % valueNail)
#         else:
#             valuesSql = valuesSql + (taskValueGroove % valueNail) + ","
#         i += 1
#
#     finalSql = insertSql + valuesSql
#     return finalSql
#
